// +----------------------------------------------------------------------
// | JavaWeb_Vue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2019~2020 南京JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <1175401194@qq.com>
// +----------------------------------------------------------------------

package com.javaweb.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.system.common.BaseQuery;
import com.javaweb.system.common.BaseServiceImpl;
import com.javaweb.system.constant.SmsnoticeConstant;
import com.javaweb.system.entity.Smsnotice;
import com.javaweb.system.mapper.SmsnoticeMapper;
import com.javaweb.system.query.SmsnoticeQuery;
import com.javaweb.system.service.ISmsnoticeService;
import com.javaweb.system.utils.ShiroUtils;
import com.javaweb.system.vo.smsnotice.SmsnoticeInfoVo;
import com.javaweb.system.vo.smsnotice.SmsnoticeListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 消息通知表 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2021-06-04
  */
@Service
public class SmsnoticeServiceImpl extends BaseServiceImpl<SmsnoticeMapper, Smsnotice> implements ISmsnoticeService {

    @Autowired
    private SmsnoticeMapper smsnoticeMapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        SmsnoticeQuery smsnoticeQuery = (SmsnoticeQuery) query;
        // 查询条件
        QueryWrapper<Smsnotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<Smsnotice> page = new Page<>(smsnoticeQuery.getPage(), smsnoticeQuery.getLimit());
        IPage<Smsnotice> pageData = smsnoticeMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            SmsnoticeListVo smsnoticeListVo = Convert.convert(SmsnoticeListVo.class, x);
            // 是否短信通知描述
            if (smsnoticeListVo.getIsSms() != null && smsnoticeListVo.getIsSms() > 0) {
                smsnoticeListVo.setIsSmsName(SmsnoticeConstant.SMSNOTICE_ISSMS_LIST.get(smsnoticeListVo.getIsSms()));
            }
            // 是否站内通知描述
            if (smsnoticeListVo.getIsSite() != null && smsnoticeListVo.getIsSite() > 0) {
                smsnoticeListVo.setIsSiteName(SmsnoticeConstant.SMSNOTICE_ISSITE_LIST.get(smsnoticeListVo.getIsSite()));
            }
            return smsnoticeListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        Smsnotice entity = (Smsnotice) super.getInfo(id);
        // 返回视图Vo
        SmsnoticeInfoVo smsnoticeInfoVo = new SmsnoticeInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, smsnoticeInfoVo);
        return smsnoticeInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(Smsnotice entity) {
        if (entity.getId() != null && entity.getId() > 0) {
            entity.setUpdateTime(DateUtils.now());
            entity.setUpdateUser(1);
        } else {
            entity.setCreateTime(DateUtils.now());
            entity.setCreateUser(1);
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(Smsnotice entity) {
        entity.setUpdateTime(DateUtils.now());
        entity.setUpdateUser(1);
        entity.setMark(0);
        return super.delete(entity);
    }

    /**
     * 设置是否短信通知
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult setIsSms(Smsnotice entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            return JsonResult.error("记录ID不能为空");
        }
        if (entity.getIsSms() == null) {
            return JsonResult.error("记录是否短信通知不能为空");
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(DateUtils.now());
        boolean result = this.updateById(entity);
        if (!result) {
            return JsonResult.error();
        }
        return JsonResult.success();
    }

    /**
     * 设置是否站内通知
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult setIsSite(Smsnotice entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            return JsonResult.error("记录ID不能为空");
        }
        if (entity.getIsSite() == null) {
            return JsonResult.error("记录是否站内通知不能为空");
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(DateUtils.now());
        boolean result = this.updateById(entity);
        if (!result) {
            return JsonResult.error();
        }
        return JsonResult.success();
    }

}